//获取应用实例
const app = getApp()

Page({
  data: {
    questionnum:0,
    afternum:0,
    ctNum:0,
    is_login: true,
    show: false,
    actions: [
      {
        name: '使用卡密激活',
      },
      {
        name: '后台已开通工种,去关联手机号',
        /**subname: '立即绑定',**/
      }
      /** 
      {
        name: '微信支付',
        subname: '暂未开通',
        disabled: true
      },
      **/
    ],
    imgUrls: [
      {
        link: '/pages/index/index',
        url: 'https://elepsoft.com/exam_wx/lunbo1.png'
        },
        {
          link: '/pages/index/index',
          url: 'https://elepsoft.com/exam_wx/lunbo2.png'
          }
      ],
      indicatorDots: true, //小点
      indicatorColor: "white",//指示点颜色
      activeColor: "coral",//当前选中的指示点颜色
      autoplay: true, //是否自动轮播
      interval: 8000, //间隔时间
      duration: 500, //滑动时间
    
  },
  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    console.log(event.detail);
    if("使用卡密激活" == event.detail.name){
        this.setData({ show: false });
        wx.navigateTo({
          url: "/pages/activation/activation"
        });

    }else if("后台已开通工种,去关联手机号" == event.detail.name){
      wx.navigateTo({
        url: "../binding/binding"
      });
    }
    
    else if("微信支付" == event.detail.name){
      console.log('==zaixian====');
    }
    
  },
  onCancel() {
    this.setData({ show: false });
  },

  onLoad: function () {
    
  },
 
  onShow:function(){
    var uid = wx.getStorageSync('uid');
    var category = wx.getStorageSync('category');
    getApp().getPermission(uid,category.id);

    this.initStorage();
    this.getCt();
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  
  initStorage:function(t){
    var category = wx.getStorageSync('category');
    var orderList = wx.getStorageSync('orderlist' + category.id);
    if(category.count == undefined || category.count == 0){
      wx.redirectTo({
        url: '/pages/category/category',
      });
      return
    }
    this.setData({
      questionnum: category.count,
      cateName: category.name,
      afternum: orderList.all == undefined ? 0 : orderList.all
    })
  },
  getCt:function(){
    var t = this
    var category = wx.getStorageSync('category');
    wx.getStorage({
      key: 'errorids' + category.id,
      success: function (e) {
        console.log(e)
        for (var a = e.data, i = "", o = 0; o < a.length; o++) a[o][Object.keys(a[o]).toString()].toString() && (i += a[o][Object.keys(a[o]).toString()].toString() + ",");
        console.info("asas", i.slice(0, -1).split(",").length), "" != i ? t.setData({
          ctNum: i.slice(0, -1).split(",").length <= 99 ? i.slice(0, -1).split(",").length : 100
        }) : t.setData({
          ctNum: 0
        });
      },
    })
  },
 

  myQuestion: function () {
    wx.navigateTo({
      url: "../category/category?isFirst=0"
    });
  },
  goSearch: function () {
    if (null == this.data.userInfo.avatarUrl || "" == this.data.userInfo.avatarUrl) {
      this.login();
      return;
    }

    if(!app.globalData.permission){
      this.setData({
        show: true
      })
       return;
    }
    wx.navigateTo({
      url: "../search/search"
    });
  },
  orderGo: function (t) {
    if (null == this.data.userInfo.avatarUrl || "" == this.data.userInfo.avatarUrl) {
      this.login();
      return;
    }
    var that = this;
    //var uid = app.globalData.uid;
    if(!app.globalData.permission){
      that.setData({
        show: true
      })
       return;
    }
    var e = 1;
      t && t.currentTarget.dataset.mode && (e = 2), setTimeout(function () {
        wx.navigateTo({
          url: '/pages/moni/moni?mode=' + e,
        })
      }, 30)

  },
  defaultGo:function(t){
    if (null == this.data.userInfo.avatarUrl || "" == this.data.userInfo.avatarUrl) {
      this.login();
      return;
    }
    var e = this;
    if(!app.globalData.permission){
      e.setData({
        show: true
      })
       return;
    }

    "0" == t.currentTarget.dataset.ind ? (setTimeout(function () {
      wx.navigateTo({
        url: "../errorpage/errorpage?ids=" + JSON.stringify(e.data.orderids),
      })
    }, 30), getApp().sectionList = JSON.stringify(this.data.orderids)) : setTimeout(function () {
        wx.navigateTo({
          url: "../collecpage/collecpage?ids=" + JSON.stringify(e.data.orderids)
        });
    }, 30)
  },
  examGo: function () {
    var that = this;
    if (null == this.data.userInfo.avatarUrl || "" == this.data.userInfo.avatarUrl) {
      this.login();
      return;
    }

    if(!app.globalData.permission){
      that.setData({
        show: true
      })
       return;
    }
    /**if (this.data.userInfo.avatarUrl == undefined || this.data.userInfo.avatarUrl == '') { //update lsd
     // if (false) {
      this.login();
      return
    }**/
    var uid = app.globalData.uid;
    wx.request({

      url: app.globalData.url + app.interfaceAddress.getSettingValue,
      method: 'get',
      dataType: 'json',
      data: {
        uid: uid,
        key: 'checkUser'
      },
      success: function (res) {
        let checkUser = false;
        if (res.data.data.value == "true")
          checkUser = true
        if (checkUser) {
          let userInfo = that.data.userInfo;
          console.log(userInfo)
          if (userInfo.status == 2) {
            setTimeout(function () {
              wx.navigateTo({
                url: '/pages/examhome/examhome',
              })
            }, 30)
          } else if (userInfo.status == 1) {
            wx.navigateTo({
              url: '../status/status',
            })
          } else {
            wx.navigateTo({
              url: '../register/register',
            })
          }
        } else {
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/examhome/examhome',
            })
          }, 30)
        }
      }
    })
    
  },
  paperGo: function () {
    setTimeout(function () {
      wx.navigateTo({
        url: "/pages/paper/paper"
      });
    }, 30);
  },
  gradeGo: function () {
    setTimeout(function () {
      wx.navigateTo({
        url: "/pages/grade/grade"
      });
    }, 30);
  },
  headerMenu: function () {
    setTimeout(function () {
      wx.navigateTo({
        url: "/pages/rank/rank"
      });
    }, 30);
  },
  login() {
    this.setData({
      is_login: !this.data.is_login
    })
  },
  getUserProfile: function (e) {
    console.log("bindgetuserinfo==========");
    this.login()
    var that = this;
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: function (res) {
        wx.showToast({
          title: "登录成功！",
          icon: 'success',
          duration: 1000
        })
        wx.setStorageSync('userInfo', res.userInfo);
        that.setData({
          userInfo: res.userInfo
        });
        var uid = wx.getStorageSync('uid');
        var params = {
          'uid': uid,
          'nickName': res.userInfo.nickName,
          'gender': res.userInfo.gender,
          'country': res.userInfo.country,
          'province': res.userInfo.province,
          'city': res.userInfo.city,
          'avatarUrl': res.userInfo.avatarUrl,
        }
       wx.request({
        url: app.globalData.url + app.interfaceAddress.perfectUserInfo,
        method: 'post',
        dataType: 'json',
        data: {
          info: params
        },
        success: function (res) {
          console.log(res)
        }
      })
      }
    })
  },
  call: function() {
    var t = [ "17752127396"];
    wx.showActionSheet({
        itemList: t,
        success: function(e) {
            wx.makePhoneCall({
                phoneNumber: t[e.tapIndex]
            });
        },
        fail: function(t) {}
    });
},
bindPhone: function () {
  if (null == this.data.userInfo.avatarUrl || "" == this.data.userInfo.avatarUrl) {
    this.login();
    return;
  }
  wx.navigateTo({
    url: "../binding/binding"
  });
},
hintTip: function () {
  wx.showModal({
    title: '温馨提示',
    content: '功能未开放，敬请期待！',
    showCancel: false
  })
},
})
